#ifndef COBROMENSUALMODULE_H
#define COBROMENSUALMODULE_H

#include <QMainWindow>
#include <QDate>
#include <QString>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlRelationalTableModel>
#include <QMessageBox>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlQuery>
#include <QSqlError>
#include <QTableView>
#include <QToolBar>
#include <QAction>
#include <QIcon>
#include <QHeaderView>
#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <QCoreApplication>
#include <QPrinter>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextImageFormat>
#include <QTextTable>
#include <QDesktopServices>
#include <QUrl>
#include <QTextTableFormat>
#include <QBrush>
#include <QColor>
#include <QTextLength>
#include <QSplitter>
#include <QGridLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QDateEdit>
#include <QRadioButton>
#include <QLabel>
#include <QLineEdit>
#include <QKeyEvent>
#include <QKeySequence>
#include <QShortcut>
#include <QScrollArea>

#include "CustomToolbar.h"
#include "VerticalScrollArea.h"
#include "EditClient.h"

class CobroMensualModule : public QMainWindow
{
    Q_OBJECT
public:
    explicit CobroMensualModule(QWidget *parent = 0);

signals:

public slots:
    void UpdateListsBtn();
    //Filtros
    void EncontrarPersona();
    void FiltroPersona();
    void EncontrarConcepto();
    void FiltroConcepto();
    void FiltrarCMFecha();
    void AbrirInfoCliente();

    void openCobroMensualClient();
    void ConfigurarCuponesClientes();
    void SeleccionarCobro(QModelIndex ind);
    void openListadoCM();

private:
    CustomModulesToolbar *cobro_mes_toolbar;
    QAction *action_cupon_mensual;
    QAction *action_todos_cupones;
    QAction *action_update_cm;
    QAction *action_listado_cm;

    //Filtros
    QString filtroCM;
    QRadioButton *filter_cliente;
    QRadioButton *filter_concepto;
    QLineEdit *find_line;
    QDateEdit *fecha_cm;
    QLineEdit *deuda_edit;
    QLineEdit *deuda_recargo_edit;

    QString cliente;

    void configCuponMensualToolBar();
    void genTodosLosCuponesToolBar();
    void ActualizarListasToolbar();
    void ListadoCobroMensualToolBar();

    bool removeDirRecursive(QString path_to_dir);

    void CalcularDeudasMes();

    void GenerarCuponesCliente(QString path);
    void GenerarCuponClientePDF(QString path, QString raz_soc);
    void GenerarListadoPDF(QString path);
    int UltimoDiaMes(int month);

    void RefreshModelView();
    void RefreshCobroMesView();

    void UpdateClientList();
    void RefreshClientCMView();
    void LimpiarListaCM();
    void ControlVencimientos();
    void ActualizarValoresCobroMensual();
};

class CobroMensualSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    CobroMensualSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        if(role == Qt::BackgroundRole)
        {
            // Fondo: Si excedente pago, darkCyan.
            QModelIndex index3 = index.model()->index(index.row(), 2, index.parent()); // get index of "watched" column
            if (index.column() != 2) {
                if (index3.data(Qt::DisplayRole).toDouble() < 0)
                    return QVariant(QColor(Qt::darkCyan));
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toDouble() < 0)
                    return QVariant(QColor(Qt::darkCyan));
            }

            // Fondo: Si pago o debe, rojo
            QModelIndex index2 = index.model()->index(index.row(), 4, index.parent()); // get index of "watched" column
            if (index.column() != 4) {
                if (index2.data(Qt::DisplayRole).toString() == "Debe")
                        return QVariant(QColor(Qt::red));
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() == "Debe")
                        return QVariant(QColor(Qt::red));
            }
        }

        if(role == Qt::ForegroundRole)
        {
            // Letras: Si pago o debe, blancas.
            QModelIndex index2 = index.model()->index(index.row(), 4, index.parent()); // get index of "watched" column
            if (index.column() != 4) {
                if (index2.data(Qt::DisplayRole).toString() == "Debe")
                        return QVariant(QColor(Qt::white));
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() == "Debe")
                        return QVariant(QColor(Qt::white));
            }
        }

        if (role == Qt::TextAlignmentRole) {
            int col = index.column();
            if (col == 2) {
                return QVariant(Qt::AlignVCenter | Qt::AlignRight);
            }
            if (col == 3) {
                return QVariant(Qt::AlignVCenter | Qt::AlignHCenter);
            }
        }

        if (role == Qt::FontRole) {
            int col = index.column();
            if (col == 2)
                return QVariant (QFont("Helvetica57-Condensed",13,13));
            else
                return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

class ClienteCMSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    ClienteCMSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        QSqlRelationalTableModel *cc_model = new QSqlRelationalTableModel();
        QString filtroCC = "estado_deuda = 'Debe'";

        if(role == Qt::BackgroundColorRole)
        {
            if (index.column() == 1) {
                QModelIndex index2 = index.model()->index(index.row(), 1, index.parent()); // get index of "watched" column
                QString razonSocial = index2.data(Qt::DisplayRole).toString();
                QString Filtro = "cc_razon_social = '" + razonSocial + "' and " + filtroCC;
                cc_model->setTable("cuenta_corriente");
                cc_model->setFilter(Filtro);

                QSqlDatabase().transaction();
                if (!cc_model->select()) {
                    QSqlDatabase().rollback();
                }
                else {
                    QSqlDatabase().commit();
                    QString rSocial;
                    int rowCount = cc_model->rowCount();
                    for (int i = 0; i < rowCount; ++i) {
                        rSocial = cc_model->index(i, 1, index.parent()).data(Qt::DisplayRole).toString();
                        if (index2.data(Qt::DisplayRole).toString() == rSocial) {
                            return QVariant(QColor(Qt::red));
                        }
                    }
                }
            }
        }

        if(role == Qt::ForegroundRole)
        {
            if (index.column() == 1) {
                QModelIndex index2 = index.model()->index(index.row(), 1, index.parent()); // get index of "watched" column
                QString razonSocial = index2.data(Qt::DisplayRole).toString();
                QString Filtro = "cc_razon_social = '" + razonSocial + "' and " + filtroCC;
                cc_model->setTable("cuenta_corriente");
                cc_model->setFilter(Filtro);

                QSqlDatabase().transaction();
                if (!cc_model->select()) {
                    QSqlDatabase().rollback();
                }
                else {
                    QSqlDatabase().commit();
                    QString rSocial;
                    int rowCount = cc_model->rowCount();
                    for (int i = 0; i < rowCount; ++i) {
                        rSocial = cc_model->index(i, 1, index.parent()).data(Qt::DisplayRole).toString();
                        if (index2.data(Qt::DisplayRole).toString() == rSocial) {
                            return QVariant(QColor(Qt::white));
                        }
                    }
                }
            }
        }

        if (role == Qt::FontRole) {
            return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

#endif // COBROMENSUALMODULE_H
